package com.itheima.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.system.Permission;
import com.itheima.domain.system.Role;
import com.itheima.domain.system.User;
import com.itheima.service.impl.system.PermissionServiceImpl;
import com.itheima.service.impl.system.RoleServiceImpl;
import com.itheima.service.system.PermissionService;
import com.itheima.service.system.RoleService;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet {

    private RoleService service = new RoleServiceImpl();
    private PermissionService permissionService =  new PermissionServiceImpl();


    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNum = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        int pageN = 1;
        int pageS = 3;
        if(pageNum != null ){
            pageN = Integer.parseInt(pageNum);
        }
        if(pageSize!=null){
            pageS = Integer.parseInt(pageSize);
        }
        List<Role> list = service.list(pageN,pageS);
        System.out.println(list);
        PageInfo<Role> pageInfo = new PageInfo<Role>(list);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/role-list.jsp").forward(req,resp);
    }

    private void toDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String rid = req.getParameter("rid");
        //获取该用户的详细信息
        Role role = service.findRole(rid);
        req.setAttribute("role",role);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/role-detail.jsp").forward(req, resp);
    }

    private void toEditPerm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String rid = req.getParameter("rid");
        List<String> uPermList = service.findPermById(rid);
        List<Permission> permList = permissionService.findAll();
        req.setAttribute("roleId",rid);
        req.setAttribute("permList",permList);
        req.setAttribute("uPermList",uPermList);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/role-permission-add.jsp").forward(req,resp);
    }

    private void addPermissionToRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String rid = req.getParameter("roleId");
        String[] ids = req.getParameterValues("ids");
        service.savePerm(rid,ids);
        resp.sendRedirect(req.getContextPath()+"/system/role/list");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.getRequestDispatcher("/WEB-INF/pages/system/role/role-add.jsp").forward(req,resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Role role = BeanUtil.fillBean(req, Role.class);
        service.save(role);
        resp.sendRedirect(req.getContextPath()+"/system/role/list");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String id = req.getParameter("id");
        service.delete(id);
        resp.sendRedirect(req.getContextPath()+"/system/role/list");
    }

    private void selectLike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String pageNum = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");

        int pageN = 1;
        int pageS = 2;
        if (pageNum != null) {
            pageN = Integer.parseInt(pageNum);
        }
        if (pageSize != null) {
            pageS = Integer.parseInt(pageSize);
        }
        String msg = req.getParameter("msg");
        List<Role> list = service.selectLike(msg,pageN,pageS);
        PageInfo<Role> pageInfo = new PageInfo<Role>(list);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/role-list.jsp").forward(req,resp);
    }
}
